package com.lebang.service;

import com.lebang.component.AliOSSComment;
import com.lebangweb.common.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Service
public class ImageSaveService {
    @Autowired
    private AliOSSComment aliOSSComment;

    @Value("${alioss.pref-task}")
    private String prefTask;
    @Value("${alioss.pref-legend}")
    private String prefLegend;
    @Value("${alioss.pref-unlegend}")
    private String prefUnLegend;

    /**
     * 保存有水印的图片
     *
     * @param filename
     * @param fileStream
     * @return
     */
    public String saveLegendImg(String filename, InputStream fileStream) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        //文件名
        String objName = prefLegend + "/" + LocalDate.now().format(df) + "/"+ UUIDUtil.L() + filename;
        boolean rt = aliOSSComment.uploadImg(objName, fileStream);
        // 给图片打水印
        aliOSSComment.imgWatermark(objName);

        return rt ? objName : null;
    }

    /**
     * 保存没有有水印的图片
     *
     * @param filename
     * @param inputStream
     * @return
     */
    public String saveUnLegendImg(String filename, InputStream inputStream) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        //文件名
        String objName = prefUnLegend + "/" + LocalDate.now().format(df) + "/"+ UUIDUtil.L() + filename;
        boolean rt = aliOSSComment.uploadImg(objName, inputStream);

        return rt ? objName : null;
    }

    public String saveTaskImg(String filename, InputStream inputStream) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        //文件名
        String objName = prefTask + "/" + LocalDate.now().format(df) + "/" + UUIDUtil.L() + filename;
        boolean rt = aliOSSComment.uploadImg(objName, inputStream);
        return rt ? objName : null;
    }
    public String saveRankImg(String filename, InputStream inputStream) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        //文件名
        String objName = "rank" + "/" + LocalDate.now().format(df) + "/" + UUIDUtil.L() + filename;
        boolean rt = aliOSSComment.uploadImg(objName, inputStream);
        return rt ? objName : null;
    }
}
